package utils

import (
	"fmt"
	"log"
)

const (
	LevelDEBUG loglevel = iota
	LevelINFO
	LevelWARN
	LevelERROR
)

var (
	pre_debug = []byte("[Debug] ")
	pre_info  = []byte("[INFO] ")
	pre_warn  = []byte("[WARN] ")
	pre_error = []byte("[ERR] ")
)

type loglevel int

type Log struct {
	level loglevel
	// out io.Writer
}

func (l *Log) SetLoglevel(level loglevel) {
	l.level = level
}

func (l *Log) Debug(a ...any) {
	if l.level <= LevelDEBUG {
		log.Print(string(fmt.Append(pre_debug, a...)))
		// _,_=fmt.Fprintf(l.out,)
	}
}

func (l *Log) Debugf(format string, a ...any) {
	if l.level <= LevelDEBUG {
		log.Print(string(fmt.Appendf(pre_debug, format, a...)))
		// _,_=fmt.Fprintf(l.out,)
	}
}

func (l *Log) Info(a ...any) {
	if l.level <= LevelINFO {
		log.Print(string(fmt.Append(pre_info, a...)))
		// _,_=fmt.Fprintf(l.out,)
	}
}

func (l *Log) Infof(format string, a ...any) {
	if l.level <= LevelINFO {
		log.Print(string(fmt.Appendf(pre_info, format, a...)))
		// _,_=fmt.Fprintf(l.out,)
	}
}

func (l *Log) Warn(a ...any) {
	if l.level <= LevelWARN {
		log.Print(string(fmt.Append(pre_warn, a...)))
		// _,_=fmt.Fprintf(l.out,)
	}
}

func (l *Log) Warnf(format string, a ...any) {
	if l.level <= LevelWARN {
		log.Print(string(fmt.Appendf(pre_warn, format, a...)))
		// _,_=fmt.Fprintf(l.out,)
	}
}

func (l *Log) Error(a ...any) {
	// if l.level <= LevelERROR {
	log.Print(string(fmt.Append(pre_error, a...)))
	// _,_=fmt.Fprintf(l.out,)
	// }
}

func (l *Log) Errorf(format string, a ...any) {
	// if l.level <= LevelERROR {
	log.Print(string(fmt.Appendf(pre_error, format, a...)))
	// _,_=fmt.Fprintf(l.out,)
	// }
}

func NewLog(level loglevel) *Log {
	// if out == nil {
	// 	out = os.Stdout
	// } else {
	// 	log.SetOutput(out)
	// }

	return &Log{
		level: level,
		// out:out,
	}
}
